home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
PET
/
S-Super PET
/
(s)t4.d64
/
TREESORT.PAS
< prev
Wrap
Pascal/Delphi Source File
|
2009-01-18
|
1KB
|
71 lines
program main( output, input );
type
nodeptr = ^node;
node =
record
left : nodeptr;
data : integer;
right : nodeptr;
end;
var
root : nodeptr;
i, j : integer;
procedure create( var pointer : nodeptr );
begin
new( pointer );
pointer^.data := i;
pointer^.left := nil;
pointer^.right := nil;
end;
procedure add( subtree : nodeptr );
begin
if( root = nil )then
create( root )
else if( subtree^.data = i )then
writeln( 'duplicate' )
else if( subtree^.data < i )then
if( subtree^.right = nil )then
create( subtree^.right )
else
add( subtree^.right )
else
if( subtree^.left = nil )then
create( subtree^.left )
else
add( subtree^.left )
end;
procedure dump( subtree : nodeptr );
begin
if( subtree <> nil )then
begin
dump( subtree^.left );
writeln( subtree^.data );
dump( subtree^.right );
end;
end;
procedure prompt;
begin
writeln( 'enter a number - "q" when done' );
end;
begin
root := nil;
prompt;
while input^ <> 'q' do
begin
readln( i );
add( root );
prompt;
end;
dump( root );
end.